package com.poxz.mybatisplus.controller;

import com.poxz.common.response.Result;
import com.poxz.common.response.ResultUtils;
import com.poxz.mybatisplus.model.dto.UserDTO;
import com.poxz.mybatisplus.model.vo.UserVO;
import com.poxz.mybatisplus.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制层
 *
 * @author poxz
 */
@RestController
@RequestMapping("/user")
@Slf4j
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 根据ID查询用户
     *
     * @param id 用户ID
     * @return Result<UserVO>
     */
    @GetMapping("/{id}")
    public Result<UserVO> getUserById(@PathVariable Long id) {
        UserVO userVO = userService.getUserById(id);
        return ResultUtils.success(userVO);
    }

    /**
     * 创建用户
     *
     * @param userDTO 用户信息
     * @return Result<UserVO>
     */
    @PostMapping
    public Result<UserVO> createUser(@Validated @RequestBody UserDTO userDTO) {
        UserVO userVO = userService.createUser(userDTO);
        return ResultUtils.success(userVO);
    }

    /**
     * 更新用户
     *
     * @param id      用户ID
     * @param userDTO 用户信息
     * @return Result<UserVO>
     */
    @PutMapping("/{id}")
    public Result<UserVO> updateUser(@PathVariable Long id, @Validated @RequestBody UserDTO userDTO) {
        UserVO userVO = userService.updateUser(id, userDTO);
        return ResultUtils.success(userVO);
    }

    /**
     * 删除用户
     *
     * @param id 用户ID
     * @return Result<Void>
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return ResultUtils.success();
    }

}
